declare namespace pm = "http://www.politicalmashup.nl";
declare namespace functx = "http://www.functx.com"; 

declare function functx:is-node-in-sequence-deep-equal 
  ( $node as node()? ,
    $seq as node()* )  as xs:boolean {
       
   some $nodeInSeq in $seq satisfies deep-equal($nodeInSeq,$node)
 } ;
 
declare function functx:distinct-deep 
  ( $nodes as node()* )  as node()* {
       
    for $seq in (1 to count($nodes))
    return $nodes[$seq][not(functx:is-node-in-sequence-deep-equal(
                          .,$nodes[position() < $seq]))]
 } ;
 
declare function functx:value-intersect 
  ( $arg1 as xs:anyAtomicType* ,
    $arg2 as xs:anyAtomicType* )  as xs:anyAtomicType* {
       
  distinct-values($arg1[.=$arg2])
 } ;
 
 declare function functx:value-except 
  ( $arg1 as xs:anyAtomicType* ,
    $arg2 as xs:anyAtomicType* )  as xs:anyAtomicType* {
       
  distinct-values($arg1[not(.=$arg2)])
 } ;
 
 declare function functx:calculate-most-popular-singleLabels 
  ( $outputByClusters as node()*,
    $speakersInvolved as xs:anyAtomicType*) as node()* {
       
    functx:distinct-deep (
    for $cluster in $outputByClusters
        for $label in $cluster/labels/label
            let $count := count (
                        functx:value-intersect (
                            $speakersInvolved,
                            for $s in $label/t/s
                                return data($s)
                        )
                    )
                    
            order by number($count) descending 
            return element label {
                element name {
                    data($label/name)
                },
                element occurences {
                    data($count) 
                }
            }
    )
 } ;  
 
declare function functx:calculate-most-popular-fullLabels 
  ( $outputByClusters as node()*,
    $speakersInvolved as xs:anyAtomicType*) as node()* {
       
    functx:distinct-deep (
        for $cluster in $outputByClusters
            let $count := count (
                            functx:value-intersect (
                                $speakersInvolved,
                                for $s in $cluster/labels/label/t/s
                                    return data($s)
                            )
                        )
                    
            order by number($count) descending 
            return element label {
                element name {
                    data($cluster/fullLabel)
                },
                element occurences {
                    data($count) 
                }
            }
    )
 } ;

let $outputByClusters := document('/db/projdb/clusterdata.xml')/clusters/cluster
let $partijen := distinct-values( document('/db/projdb/lijst_van_sprekers_ge2009.xml')/speakers/speaker/partij )
let $allSpeakers := document('/db/projdb/lijst_van_sprekers_ge2009.xml')/speakers/speaker/name
let $burgemeesters := document('/db/projdb/lijst_burgemeesters_2007.xml')/burgemeesters/tupel/burgemeester
let $niet-burgemeesters := functx:value-except($allSpeakers, $burgemeesters)
let $mannen := document('/db/projdb/lijst_van_sprekers_ge2009.xml')/speakers/speaker[./sex = "M"]/name
let $vrouwen := document('/db/projdb/lijst_van_sprekers_ge2009.xml')/speakers/speaker[./sex = "F"]/name



let $singleLabels := element singleLabels {
    element everyone {
        functx:calculate-most-popular-singleLabels($outputByClusters, $allSpeakers )
    }, element partijen{
        for $partij in $partijen
            let $speakersVanPartij := document('/db/projdb/lijst_van_sprekers_ge2009.xml')/speakers/speaker[./partij = $partij ]/name
            return element partij {
                element name {
                    data( $partij )
                },
                element labels {
                    functx:calculate-most-popular-singleLabels($outputByClusters, $speakersVanPartij)
                }
            }
    }, element burgemeesters {
        functx:calculate-most-popular-singleLabels($outputByClusters, $burgemeesters)
    }, element nietBurgemeesters {
        functx:calculate-most-popular-singleLabels($outputByClusters, $niet-burgemeesters )
    }, element mannen {
        functx:calculate-most-popular-singleLabels($outputByClusters, $mannen )
    }, element vrouwen {
        functx:calculate-most-popular-singleLabels($outputByClusters, $vrouwen )
    }
}



let $fullLabels := element fullLabels {
    element everyone {
        functx:calculate-most-popular-fullLabels($outputByClusters, $allSpeakers)
    }, element partijen{
        for $partij in $partijen
            let $speakersVanPartij := document('/db/projdb/lijst_van_sprekers_ge2009.xml')/speakers/speaker[./partij = $partij ]/name
            return element partij {
                element name {
                    data( $partij )
                },
                element labels {
                    functx:calculate-most-popular-fullLabels($outputByClusters, $speakersVanPartij)
                }
            }
    }, element burgemeesters {
        functx:calculate-most-popular-fullLabels($outputByClusters, $burgemeesters)
    }, element nietBurgemeesters {
        functx:calculate-most-popular-fullLabels($outputByClusters, $niet-burgemeesters )
    }, element mannen {
        functx:calculate-most-popular-fullLabels($outputByClusters, $mannen )
    }, element vrouwen {
        functx:calculate-most-popular-fullLabels($outputByClusters, $vrouwen )
    }
}



let $collection := 'xmldb:exist:///db/projdb'
let $file-name := 'clusterdataMostPopular.xml'
let $result := element result {
    $singleLabels,
    $fullLabels
}

let $store-return-status := xmldb:store($collection, $file-name, $result )

return <message>New Document Created {$store-return-status} at {$collection}/{$file-name}</message>
